{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import random\n",
    "#忽略警号\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:09:48.278133300Z",
     "start_time": "2024-09-23T13:09:22.293761800Z"
    }
   },
   "id": "955def2dff9332a5",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:51:09.979087200Z",
     "start_time": "2024-09-23T13:51:09.731924100Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  age_range  gender\n0   376517        6.0     1.0\n1   234512        5.0     0.0\n2   344532        5.0     0.0\n3   186135        5.0     0.0\n4    30230        5.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>376517</td>\n      <td>6.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>234512</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>344532</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>186135</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>30230</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = pd.read_csv('user_info_format1.csv')\n",
    "user_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type\n0   328862   323294     833       2882    2661.0         829            0\n1   328862   844400    1271       2882    2661.0         829            0\n2   328862   575153    1271       2882    2661.0         829            0\n3   328862   996875    1271       2882    2661.0         829            0\n4   328862  1086186    1271       1253    1049.0         829            0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>seller_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>328862</td>\n      <td>323294</td>\n      <td>833</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>328862</td>\n      <td>844400</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>328862</td>\n      <td>575153</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>328862</td>\n      <td>996875</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>328862</td>\n      <td>1086186</td>\n      <td>1271</td>\n      <td>1253</td>\n      <td>1049.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log = pd.read_csv('user_log_format1.csv')\n",
    "user_log.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:17.174740100Z",
     "start_time": "2024-09-23T13:51:12.396518400Z"
    }
   },
   "id": "2c525cb895b58ac5",
   "execution_count": 52
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id         0\nage_range    2217\ngender       6436\ndtype: int64"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:11:06.522872Z",
     "start_time": "2024-09-23T12:11:06.401794100Z"
    }
   },
   "id": "2e9d2aa998b4b3b9",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id            0\nitem_id            0\ncat_id             0\nseller_id          0\nbrand_id       91015\ntime_stamp         0\naction_type        0\ndtype: int64"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.isnull().sum() "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:11:13.013087600Z",
     "start_time": "2024-09-23T12:11:11.974909600Z"
    }
   },
   "id": "b52a126a0523a131",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424170 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    424170 non-null  int64  \n",
      " 1   age_range  421953 non-null  float64\n",
      " 2   gender     417734 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 9.7 MB\n"
     ]
    }
   ],
   "source": [
    "user_info.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:11:44.044609500Z",
     "start_time": "2024-09-23T12:11:43.689120800Z"
    }
   },
   "id": "7dffd89587d5fc0f",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 54925330 entries, 0 to 54925329\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   seller_id    int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   int64  \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(6)\n",
      "memory usage: 2.9 GB\n"
     ]
    }
   ],
   "source": [
    "user_log.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:11:47.511681600Z",
     "start_time": "2024-09-23T12:11:47.430629Z"
    }
   },
   "id": "a57b928566bc0c2e",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(54925330, 7)"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:11:51.084978700Z",
     "start_time": "2024-09-23T12:11:51.008927500Z"
    }
   },
   "id": "78ef6fdac1a8b2ed",
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id      0\nage_range    0\ngender       0\ndtype: int64"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除空值\n",
    "user_info['age_range'].replace(np.nan,2,inplace=True) # 2和NULL表示未知\n",
    "user_info['gender'].replace(np.nan,-1,inplace=True)\n",
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:24.923012300Z",
     "start_time": "2024-09-23T13:52:24.766911Z"
    }
   },
   "id": "fb4b8f869391b193",
   "execution_count": 53
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "98f6aa45b17a8b8c"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id        0\nitem_id        0\ncat_id         0\nseller_id      0\nbrand_id       0\ntime_stamp     0\naction_type    0\ndtype: int64"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log['brand_id'].replace(np.nan,-1,inplace=True)\n",
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:30.009377900Z",
     "start_time": "2024-09-23T13:52:27.656822Z"
    }
   },
   "id": "26c9ebd5dccdc3c3",
   "execution_count": 54
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "13750198\n"
     ]
    }
   ],
   "source": [
    "print(user_info.duplicated().sum())\n",
    "print(user_log.duplicated().sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T11:36:03.389535800Z",
     "start_time": "2024-09-23T11:34:56.585637100Z"
    }
   },
   "id": "b05a9703db44e02a",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# user_log.drop_duplicates(inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-20T06:16:22.088839800Z",
     "start_time": "2024-09-20T06:15:26.813650400Z"
    }
   },
   "id": "ea2c96927e2f314b",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label\n0    34176         3906      0\n1    34176          121      0\n2    34176         4356      1\n3    34176         2217      0\n4   230784         4818      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('train_format1.csv')\n",
    "train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:33.058917600Z",
     "start_time": "2024-09-23T13:52:32.735703400Z"
    }
   },
   "id": "75111d5dd5f7e736",
   "execution_count": 55
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  prob\n0   163968         4605   NaN\n1   360576         1581   NaN\n2    98688         1964   NaN\n3    98688         3645   NaN\n4   295296         3361   NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>prob</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>163968</td>\n      <td>4605</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>360576</td>\n      <td>1581</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>98688</td>\n      <td>1964</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>98688</td>\n      <td>3645</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>295296</td>\n      <td>3361</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = pd.read_csv('test_format1.csv')\n",
    "test.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:36.331966Z",
     "start_time": "2024-09-23T13:52:35.877415600Z"
    }
   },
   "id": "940d24725d2d3f28",
   "execution_count": 56
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "train['origin'] = 'train'\n",
    "test['origin'] = 'test'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:38.919773100Z",
     "start_time": "2024-09-23T13:52:38.806699Z"
    }
   },
   "id": "b8e752eeea806dab",
   "execution_count": 57
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 连接train、test表\n",
    "data = pd.concat([train, test], ignore_index=True, sort=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:41.151248900Z",
     "start_time": "2024-09-23T13:52:40.998148400Z"
    }
   },
   "id": "d76f37761daecf20",
   "execution_count": 58
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user_log.rename(columns={'seller_id':'merchant_id'},inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:44.389807500Z",
     "start_time": "2024-09-23T13:52:44.297708700Z"
    }
   },
   "id": "9df793452e733728",
   "execution_count": 59
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label origin  prob  age_range  gender\n0    34176         3906    0.0  train   NaN        6.0     0.0\n1    34176          121    0.0  train   NaN        6.0     0.0\n2    34176         4356    1.0  train   NaN        6.0     0.0\n3    34176         2217    0.0  train   NaN        6.0     0.0\n4   230784         4818    0.0  train   NaN        0.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>prob</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.merge(data,user_info, on='user_id')\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:46.912899900Z",
     "start_time": "2024-09-23T13:52:46.295491800Z"
    }
   },
   "id": "a4a491529289015f",
   "execution_count": 60
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data.drop(['prob'],axis=1,inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:50.097609800Z",
     "start_time": "2024-09-23T13:52:49.972526300Z"
    }
   },
   "id": "e22e12b1d0cda6ae",
   "execution_count": 61
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label origin  age_range  gender\n0    34176         3906    0.0  train        6.0     0.0\n1    34176          121    0.0  train        6.0     0.0\n2    34176         4356    1.0  train        6.0     0.0\n3    34176         2217    0.0  train        6.0     0.0\n4   230784         4818    0.0  train        0.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:12:00.210449500Z",
     "start_time": "2024-09-23T13:12:00.049342900Z"
    }
   },
   "id": "dfdf0d25ad0c9d71",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 522341 entries, 0 to 522340\n",
      "Data columns (total 6 columns):\n",
      " #   Column       Non-Null Count   Dtype \n",
      "---  ------       --------------   ----- \n",
      " 0   user_id      522341 non-null  int32 \n",
      " 1   merchant_id  522341 non-null  int32 \n",
      " 2   label        522341 non-null  object\n",
      " 3   origin       522341 non-null  object\n",
      " 4   age_range    522341 non-null  int8  \n",
      " 5   gender       522341 non-null  int8  \n",
      "dtypes: int32(2), int8(2), object(2)\n",
      "memory usage: 13.0+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:26:30.693623200Z",
     "start_time": "2024-09-23T12:26:30.573543500Z"
    }
   },
   "id": "8c8f19324e766e4d",
   "execution_count": 48
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 选取的特征\n",
    "用户的年龄(age_range)\n",
    "用户的性别(gender)\n",
    "某用户在该商家日志的总条数(total_logs)\n",
    "用户浏览的商品的数目，就是浏览了多少个商品(unique_item_ids)\n",
    "浏览的商品的种类的数目，就是浏览了多少种商品(categories)\n",
    "用户浏览的天数(browse_days)\n",
    "用户单击的次数(one_clicks)\n",
    "用户添加购物车的次数(shopping_carts)\n",
    "用户购买的次数(purchase_times)\n",
    "用户收藏的次数(favourite_times)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f372c652392c9afc"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user_log.rename(columns={'seller_id':'merchant_id'},inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:52:55.290567100Z",
     "start_time": "2024-09-23T13:52:55.241535Z"
    }
   },
   "id": "357535d4b544c316",
   "execution_count": 62
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 某用户在该商家日志的总条数(total_logs)\n",
    "total_logs = user_log.groupby([user_log['user_id'],user_log['merchant_id']]).count().reset_index()[['user_id','merchant_id','item_id']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:53:19.364528Z",
     "start_time": "2024-09-23T13:52:57.140359600Z"
    }
   },
   "id": "f1ca6388f423e4f9",
   "execution_count": 63
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  total_logs\n0        1          471           1\n1        1          739           1\n2        1          925           4\n3        1         1019          14\n4        1         1156           1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>total_logs</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>14</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_logs.rename(columns={\"item_id\":\"total_logs\"},inplace=True)\n",
    "total_logs.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:53:24.349567800Z",
     "start_time": "2024-09-23T13:53:24.261510Z"
    }
   },
   "id": "47f95ad6cda6838",
   "execution_count": 64
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label origin  age_range  gender  total_logs\n0    34176         3906    0.0  train        6.0     0.0          39\n1    34176          121    0.0  train        6.0     0.0          14\n2    34176         4356    1.0  train        6.0     0.0          18\n3    34176         2217    0.0  train        6.0     0.0           2\n4   230784         4818    0.0  train        0.0     0.0           8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>14</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.merge(data,total_logs,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:53:34.802694Z",
     "start_time": "2024-09-23T13:53:26.882013200Z"
    }
   },
   "id": "216f675c529a3679",
   "execution_count": 65
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 用户浏览的商品的数目(unique_item_ids)\n",
    "unique_item_ids_tmp = user_log.groupby([user_log['user_id'],user_log['merchant_id'],user_log['item_id']]).count().reset_index()[['user_id','merchant_id']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:54:26.654363200Z",
     "start_time": "2024-09-23T13:53:37.869198800Z"
    }
   },
   "id": "90c231817110ead8",
   "execution_count": 66
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id\n0        1          471\n1        1          739\n2        1          925\n3        1         1019\n4        1         1156",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique_item_ids_tmp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:54:29.205872800Z",
     "start_time": "2024-09-23T13:54:29.049766300Z"
    }
   },
   "id": "9624fd79706bbae3",
   "execution_count": 67
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "unique_item_ids_tmp['cnt'] = 1\n",
    "unique_item_ids = unique_item_ids_tmp.groupby([unique_item_ids_tmp[\"user_id\"],unique_item_ids_tmp[\"merchant_id\"]]).count().reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:54:39.295905500Z",
     "start_time": "2024-09-23T13:54:31.743911500Z"
    }
   },
   "id": "e64085029a4781d8",
   "execution_count": 68
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  unique_item_ids\n0        1          471                1\n1        1          739                1\n2        1          925                1\n3        1         1019                1\n4        1         1156                1\n5        1         2245                4\n6        1         4026                1\n7        1         4177                1\n8        1         4335                1\n9        2          420               15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>unique_item_ids</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2</td>\n      <td>420</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique_item_ids.rename(columns={\"cnt\":\"unique_item_ids\"},inplace=True)\n",
    "unique_item_ids.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:54:48.378390400Z",
     "start_time": "2024-09-23T13:54:48.227291300Z"
    }
   },
   "id": "f26a49d9cd2eef0a",
   "execution_count": 69
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label origin  age_range  gender  total_logs  \\\n0    34176         3906    0.0  train        6.0     0.0          39   \n1    34176          121    0.0  train        6.0     0.0          14   \n2    34176         4356    1.0  train        6.0     0.0          18   \n3    34176         2217    0.0  train        6.0     0.0           2   \n4   230784         4818    0.0  train        0.0     0.0           8   \n\n   unique_item_ids_x  unique_item_ids_y  \n0                 20                 20  \n1                  1                  1  \n2                  2                  2  \n3                  1                  1  \n4                  1                  1  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids_x</th>\n      <th>unique_item_ids_y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>14</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>18</td>\n      <td>2</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.merge(data,unique_item_ids,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:55:20.418303200Z",
     "start_time": "2024-09-23T13:55:12.201868100Z"
    }
   },
   "id": "998af52818e103cf",
   "execution_count": 71
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 浏览的商品的种类的数目(categories)\n",
    "categories = user_log.groupby([user_log[\"user_id\"],user_log[\"merchant_id\"],user_log[\"cat_id\"]]).count().reset_index()[[\"user_id\",\"merchant_id\",'item_id']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:55:52.876474600Z",
     "start_time": "2024-09-23T13:55:22.458771400Z"
    }
   },
   "id": "26bf1982420f3667",
   "execution_count": 72
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  categories\n0        1          471           1\n1        1          739           1\n2        1          925           4\n3        1         1019          14\n4        1         1156           1\n5        1         2245           5\n6        1         4026           5\n7        1         4177           1\n8        1         4335           1\n9        2          420          18",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>categories</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>14</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2</td>\n      <td>420</td>\n      <td>18</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categories.rename(columns={\"item_id\":\"categories\"},inplace=True)\n",
    "categories.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:56:14.458957Z",
     "start_time": "2024-09-23T13:56:14.320865200Z"
    }
   },
   "id": "e7556a835e678ed8",
   "execution_count": 74
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label origin  age_range  gender  total_logs  \\\n0    34176         3906    0.0  train        6.0     0.0          39   \n1    34176         3906    0.0  train        6.0     0.0          39   \n2    34176         3906    0.0  train        6.0     0.0          39   \n3    34176         3906    0.0  train        6.0     0.0          39   \n4    34176         3906    0.0  train        6.0     0.0          39   \n\n   unique_item_ids_x  unique_item_ids_y  categories  \n0                 20                 20           1  \n1                 20                 20           7  \n2                 20                 20          24  \n3                 20                 20           1  \n4                 20                 20           2  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids_x</th>\n      <th>unique_item_ids_y</th>\n      <th>categories</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.merge(data,categories,on=['user_id','merchant_id'],how='left')\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:56:26.529865400Z",
     "start_time": "2024-09-23T13:56:18.078274400Z"
    }
   },
   "id": "c60a095c15849cac",
   "execution_count": 75
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  time_stamp\n0        1          471        1111\n1        1          739        1018\n2        1          925        1011\n3        1         1019        1111\n4        1         1156        1111\n5        1         2245        1009\n6        1         4026        1018\n7        1         4026        1021\n8        1         4177        1018\n9        1         4335        1111",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>time_stamp</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1111</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1018</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>1011</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>1111</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1111</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>1009</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>1018</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>1021</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>1018</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>1111</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用户浏览的天数(browse_days)\n",
    "browse_days_temp = user_log.groupby([user_log[\"user_id\"],user_log[\"merchant_id\"],user_log[\"time_stamp\"]]).count().reset_index()[[\"user_id\",\"merchant_id\",\"time_stamp\"]]\n",
    "browse_days_temp.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:57:07.250391Z",
     "start_time": "2024-09-23T13:56:31.830814400Z"
    }
   },
   "id": "f86771ad36d1c21c",
   "execution_count": 76
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  time_stamp\n0        1          471           1\n1        1          739           1\n2        1          925           1\n3        1         1019           1\n4        1         1156           1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>time_stamp</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "browse_days_temp1 = browse_days_temp.groupby([browse_days_temp[\"user_id\"],browse_days_temp[\"merchant_id\"]]).count().reset_index()\n",
    "browse_days_temp1.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:57:17.964702200Z",
     "start_time": "2024-09-23T13:57:12.609159700Z"
    }
   },
   "id": "e13cd3d6b52b355c",
   "execution_count": 77
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  browse_days\n0        1          471            1\n1        1          739            1\n2        1          925            1\n3        1         1019            1\n4        1         1156            1\n5        1         2245            1\n6        1         4026            2\n7        1         4177            1\n8        1         4335            1\n9        2          420            1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>browse_days</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2</td>\n      <td>420</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "browse_days_temp1.rename(columns={\"time_stamp\":\"browse_days\"},inplace=True)\n",
    "browse_days_temp1.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:57:21.178873100Z",
     "start_time": "2024-09-23T13:57:20.914697100Z"
    }
   },
   "id": "95baec721aa0b64f",
   "execution_count": 78
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label origin  age_range  gender  total_logs  \\\n0    34176         3906    0.0  train        6.0     0.0          39   \n1    34176         3906    0.0  train        6.0     0.0          39   \n2    34176         3906    0.0  train        6.0     0.0          39   \n3    34176         3906    0.0  train        6.0     0.0          39   \n4    34176         3906    0.0  train        6.0     0.0          39   \n5    34176         3906    0.0  train        6.0     0.0          39   \n6    34176          121    0.0  train        6.0     0.0          14   \n7    34176         4356    1.0  train        6.0     0.0          18   \n8    34176         2217    0.0  train        6.0     0.0           2   \n9   230784         4818    0.0  train        0.0     0.0           8   \n\n   unique_item_ids_x  unique_item_ids_y  categories  browse_days  \n0                 20                 20           1            9  \n1                 20                 20           7            9  \n2                 20                 20          24            9  \n3                 20                 20           1            9  \n4                 20                 20           2            9  \n5                 20                 20           4            9  \n6                  1                  1          14            3  \n7                  2                  2          18            2  \n8                  1                  1           2            1  \n9                  1                  1           8            3  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids_x</th>\n      <th>unique_item_ids_y</th>\n      <th>categories</th>\n      <th>browse_days</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>7</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>24</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>2</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>4</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>14</td>\n      <td>1</td>\n      <td>1</td>\n      <td>14</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>18</td>\n      <td>2</td>\n      <td>2</td>\n      <td>18</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n      <td>1</td>\n      <td>1</td>\n      <td>8</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.merge(data,browse_days_temp1,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "data.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:57:34.142197500Z",
     "start_time": "2024-09-23T13:57:25.339774Z"
    }
   },
   "id": "2203df1d3ed25168",
   "execution_count": 79
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  item_id\n0        1          471            0        1\n1        1          739            0        1\n2        1          925            0        3\n3        1          925            2        1\n4        1         1019            0       10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>item_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>0</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_clicks_temp = user_log.groupby([user_log[\"user_id\"], user_log[\"merchant_id\"], user_log[\"action_type\"]]).count().reset_index()[\n",
    "    [\"user_id\", \"merchant_id\", \"action_type\", \"item_id\"]]\n",
    "one_clicks_temp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:12.825006600Z",
     "start_time": "2024-09-23T13:57:38.163693400Z"
    }
   },
   "id": "6f2b8f248c76e9bd",
   "execution_count": 80
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  times\n0        1          471            0      1\n1        1          739            0      1\n2        1          925            0      3\n3        1          925            2      1\n4        1         1019            0     10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>0</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_clicks_temp.rename(columns={\"item_id\": \"times\"}, inplace=True)\n",
    "one_clicks_temp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:14.738273500Z",
     "start_time": "2024-09-23T13:58:14.605185500Z"
    }
   },
   "id": "8e92ea785aeb93e2",
   "execution_count": 81
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  times  one_clicks\n0        1          471            0      1           1\n1        1          739            0      1           1\n2        1          925            0      3           3\n3        1          925            2      1           0\n4        1         1019            0     10          10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>times</th>\n      <th>one_clicks</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>0</td>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>0</td>\n      <td>10</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_clicks_temp[\"one_clicks\"] = one_clicks_temp[\"action_type\"] == 0\n",
    "one_clicks_temp[\"one_clicks\"] = one_clicks_temp[\"one_clicks\"] * one_clicks_temp[\"times\"]\n",
    "one_clicks_temp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:18.202548700Z",
     "start_time": "2024-09-23T13:58:17.772264Z"
    }
   },
   "id": "7985d713b1f8fd8e",
   "execution_count": 82
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  times  one_clicks  shopping_carts\n0        1          471            0      1           1               0\n1        1          739            0      1           1               0\n2        1          925            0      3           3               0\n3        1          925            2      1           0               0\n4        1         1019            0     10          10               0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>times</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>0</td>\n      <td>3</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>0</td>\n      <td>10</td>\n      <td>10</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_clicks_temp[\"shopping_carts\"] = one_clicks_temp[\"action_type\"] == 1\n",
    "one_clicks_temp[\"shopping_carts\"] = one_clicks_temp[\"shopping_carts\"] * one_clicks_temp[\"times\"]\n",
    "one_clicks_temp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:21.549763Z",
     "start_time": "2024-09-23T13:58:21.153501600Z"
    }
   },
   "id": "3984f4d7754a0aae",
   "execution_count": 83
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  times  one_clicks  shopping_carts  \\\n0        1          471            0      1           1               0   \n1        1          739            0      1           1               0   \n2        1          925            0      3           3               0   \n3        1          925            2      1           0               0   \n4        1         1019            0     10          10               0   \n\n   purchase_times  \n0               0  \n1               0  \n2               0  \n3               1  \n4               0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>times</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n      <th>purchase_times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>0</td>\n      <td>3</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>0</td>\n      <td>10</td>\n      <td>10</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_clicks_temp[\"purchase_times\"] = one_clicks_temp[\"action_type\"] == 2\n",
    "one_clicks_temp[\"purchase_times\"] = one_clicks_temp[\"purchase_times\"] * one_clicks_temp[\"times\"]\n",
    "one_clicks_temp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:25.409316200Z",
     "start_time": "2024-09-23T13:58:24.767892700Z"
    }
   },
   "id": "f6800cf1aede1a46",
   "execution_count": 84
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  times  one_clicks  shopping_carts  \\\n0        1          471            0      1           1               0   \n1        1          739            0      1           1               0   \n2        1          925            0      3           3               0   \n3        1          925            2      1           0               0   \n4        1         1019            0     10          10               0   \n\n   purchase_times  favourite_times  \n0               0                0  \n1               0                0  \n2               0                0  \n3               1                0  \n4               0                0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>times</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n      <th>purchase_times</th>\n      <th>favourite_times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>0</td>\n      <td>3</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>0</td>\n      <td>10</td>\n      <td>10</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_clicks_temp[\"favourite_times\"] = one_clicks_temp[\"action_type\"] == 3\n",
    "one_clicks_temp[\"favourite_times\"] = one_clicks_temp[\"favourite_times\"] * one_clicks_temp[\"times\"]\n",
    "one_clicks_temp.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:29.846250300Z",
     "start_time": "2024-09-23T13:58:28.134120400Z"
    }
   },
   "id": "779acbd7e7c9d677",
   "execution_count": 85
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  action_type  times  one_clicks  shopping_carts  \\\n0        1          471            0      1           1               0   \n1        1          739            0      1           1               0   \n2        1          925            2      4           3               0   \n3        1         1019            2     14          10               0   \n4        1         1156            0      1           1               0   \n5        1         2245            0      5           5               0   \n6        1         4026            2      5           4               0   \n7        1         4177            0      1           1               0   \n8        1         4335            0      1           1               0   \n9        2          420            2     26          23               0   \n\n   purchase_times  favourite_times  \n0               0                0  \n1               0                0  \n2               1                0  \n3               4                0  \n4               0                0  \n5               0                0  \n6               1                0  \n7               0                0  \n8               0                0  \n9               3                0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>action_type</th>\n      <th>times</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n      <th>purchase_times</th>\n      <th>favourite_times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>2</td>\n      <td>4</td>\n      <td>3</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>2</td>\n      <td>14</td>\n      <td>10</td>\n      <td>0</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>2245</td>\n      <td>0</td>\n      <td>5</td>\n      <td>5</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1</td>\n      <td>4026</td>\n      <td>2</td>\n      <td>5</td>\n      <td>4</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1</td>\n      <td>4177</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1</td>\n      <td>4335</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2</td>\n      <td>420</td>\n      <td>2</td>\n      <td>26</td>\n      <td>23</td>\n      <td>0</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "four_features = one_clicks_temp.groupby(\n",
    "    [one_clicks_temp[\"user_id\"], one_clicks_temp[\"merchant_id\"]]).sum().reset_index()\n",
    "four_features.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:45.553273Z",
     "start_time": "2024-09-23T13:58:32.524653300Z"
    }
   },
   "id": "57816616c2e9b17e",
   "execution_count": 86
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  one_clicks  shopping_carts  purchase_times  \\\n0        1          471           1               0               0   \n1        1          739           1               0               0   \n2        1          925           3               0               1   \n3        1         1019          10               0               4   \n4        1         1156           1               0               0   \n\n   favourite_times  \n0                0  \n1                0  \n2                0  \n3                0  \n4                0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n      <th>purchase_times</th>\n      <th>favourite_times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>471</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>739</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>925</td>\n      <td>3</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1019</td>\n      <td>10</td>\n      <td>0</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1156</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "four_features = four_features.drop([\"action_type\", \"times\"], axis=1)\n",
    "four_features.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:58:46.348797300Z",
     "start_time": "2024-09-23T13:58:45.554273700Z"
    }
   },
   "id": "8a95bb7533e93425",
   "execution_count": 87
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "    user_id  merchant_id  label origin  age_range  gender  total_logs  \\\n0     34176         3906    0.0  train        6.0     0.0          39   \n1     34176         3906    0.0  train        6.0     0.0          39   \n2     34176         3906    0.0  train        6.0     0.0          39   \n3     34176         3906    0.0  train        6.0     0.0          39   \n4     34176         3906    0.0  train        6.0     0.0          39   \n5     34176         3906    0.0  train        6.0     0.0          39   \n6     34176          121    0.0  train        6.0     0.0          14   \n7     34176         4356    1.0  train        6.0     0.0          18   \n8     34176         2217    0.0  train        6.0     0.0           2   \n9    230784         4818    0.0  train        0.0     0.0           8   \n10   362112         2618    0.0  train        4.0     1.0           1   \n11    34944         2051    0.0  train        5.0     0.0           3   \n12   231552         3828    1.0  train        5.0     0.0          83   \n13   231552         3828    1.0  train        5.0     0.0          83   \n14   231552         3828    1.0  train        5.0     0.0          83   \n15   231552         3828    1.0  train        5.0     0.0          83   \n16   231552         3828    1.0  train        5.0     0.0          83   \n17   231552         3828    1.0  train        5.0     0.0          83   \n18   231552         3828    1.0  train        5.0     0.0          83   \n19   231552         3828    1.0  train        5.0     0.0          83   \n20   231552         3828    1.0  train        5.0     0.0          83   \n21   231552         3828    1.0  train        5.0     0.0          83   \n22   231552         3828    1.0  train        5.0     0.0          83   \n23   231552         3828    1.0  train        5.0     0.0          83   \n24   231552         3828    1.0  train        5.0     0.0          83   \n25   231552         3828    1.0  train        5.0     0.0          83   \n26   231552         3828    1.0  train        5.0     0.0          83   \n27   231552         2124    0.0  train        5.0     0.0           7   \n28   232320         1168    0.0  train        4.0     1.0           4   \n29   232320         4270    0.0  train        4.0     1.0          22   \n\n    unique_item_ids_x  unique_item_ids_y  categories  browse_days  one_clicks  \\\n0                  20                 20           1            9          36   \n1                  20                 20           7            9          36   \n2                  20                 20          24            9          36   \n3                  20                 20           1            9          36   \n4                  20                 20           2            9          36   \n5                  20                 20           4            9          36   \n6                   1                  1          14            3          13   \n7                   2                  2          18            2          12   \n8                   1                  1           2            1           1   \n9                   1                  1           8            3           7   \n10                  1                  1           1            1           0   \n11                  2                  2           3            1           2   \n12                 48                 48           1            3          78   \n13                 48                 48           2            3          78   \n14                 48                 48           8            3          78   \n15                 48                 48           3            3          78   \n16                 48                 48           4            3          78   \n17                 48                 48           2            3          78   \n18                 48                 48           3            3          78   \n19                 48                 48           8            3          78   \n20                 48                 48           2            3          78   \n21                 48                 48           2            3          78   \n22                 48                 48           5            3          78   \n23                 48                 48           1            3          78   \n24                 48                 48          22            3          78   \n25                 48                 48          11            3          78   \n26                 48                 48           9            3          78   \n27                  4                  4           7            1           6   \n28                  1                  1           4            2           2   \n29                 13                 13           5            2          13   \n\n    shopping_carts  purchase_times  favourite_times  \n0                0               1                2  \n1                0               1                2  \n2                0               1                2  \n3                0               1                2  \n4                0               1                2  \n5                0               1                2  \n6                0               1                0  \n7                0               6                0  \n8                0               1                0  \n9                0               1                0  \n10               0               1                0  \n11               0               1                0  \n12               0               5                0  \n13               0               5                0  \n14               0               5                0  \n15               0               5                0  \n16               0               5                0  \n17               0               5                0  \n18               0               5                0  \n19               0               5                0  \n20               0               5                0  \n21               0               5                0  \n22               0               5                0  \n23               0               5                0  \n24               0               5                0  \n25               0               5                0  \n26               0               5                0  \n27               0               1                0  \n28               0               1                1  \n29               0               2                7  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids_x</th>\n      <th>unique_item_ids_y</th>\n      <th>categories</th>\n      <th>browse_days</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n      <th>purchase_times</th>\n      <th>favourite_times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>7</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>24</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>2</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>4</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>14</td>\n      <td>1</td>\n      <td>1</td>\n      <td>14</td>\n      <td>3</td>\n      <td>13</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>18</td>\n      <td>2</td>\n      <td>2</td>\n      <td>18</td>\n      <td>2</td>\n      <td>12</td>\n      <td>0</td>\n      <td>6</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>8</td>\n      <td>1</td>\n      <td>1</td>\n      <td>8</td>\n      <td>3</td>\n      <td>7</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>362112</td>\n      <td>2618</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>4.0</td>\n      <td>1.0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>34944</td>\n      <td>2051</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>3</td>\n      <td>2</td>\n      <td>2</td>\n      <td>3</td>\n      <td>1</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>1</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>2</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>8</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>3</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>4</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>2</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>3</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>8</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>20</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>2</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>21</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>2</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>22</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>5</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>23</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>1</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>24</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>22</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>25</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>11</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>26</th>\n      <td>231552</td>\n      <td>3828</td>\n      <td>1.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>83</td>\n      <td>48</td>\n      <td>48</td>\n      <td>9</td>\n      <td>3</td>\n      <td>78</td>\n      <td>0</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>27</th>\n      <td>231552</td>\n      <td>2124</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>5.0</td>\n      <td>0.0</td>\n      <td>7</td>\n      <td>4</td>\n      <td>4</td>\n      <td>7</td>\n      <td>1</td>\n      <td>6</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>232320</td>\n      <td>1168</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>4.0</td>\n      <td>1.0</td>\n      <td>4</td>\n      <td>1</td>\n      <td>1</td>\n      <td>4</td>\n      <td>2</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>29</th>\n      <td>232320</td>\n      <td>4270</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>4.0</td>\n      <td>1.0</td>\n      <td>22</td>\n      <td>13</td>\n      <td>13</td>\n      <td>5</td>\n      <td>2</td>\n      <td>13</td>\n      <td>0</td>\n      <td>2</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.merge(data,four_features,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "data.head(30)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:59:19.451485Z",
     "start_time": "2024-09-23T13:59:10.761724500Z"
    }
   },
   "id": "27592b8c5d006827",
   "execution_count": 89
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "    user_id  merchant_id  label origin  age_range  gender  total_logs  \\\n0     34176         3906    0.0  train        6.0     0.0          39   \n1     34176         3906    0.0  train        6.0     0.0          39   \n2     34176         3906    0.0  train        6.0     0.0          39   \n3     34176         3906    0.0  train        6.0     0.0          39   \n4     34176         3906    0.0  train        6.0     0.0          39   \n..      ...          ...    ...    ...        ...     ...         ...   \n95   305280          630    0.0  train        3.0     0.0          20   \n96   305280          630    0.0  train        3.0     0.0          20   \n97   305280          630    0.0  train        3.0     0.0          20   \n98   305280          630    0.0  train        3.0     0.0          20   \n99   240000         2278    0.0  train        3.0     0.0           2   \n\n    unique_item_ids_x  unique_item_ids_y  categories  browse_days  one_clicks  \\\n0                  20                 20           1            9          36   \n1                  20                 20           7            9          36   \n2                  20                 20          24            9          36   \n3                  20                 20           1            9          36   \n4                  20                 20           2            9          36   \n..                ...                ...         ...          ...         ...   \n95                  4                  4           1            3          15   \n96                  4                  4           3            3          15   \n97                  4                  4           8            3          15   \n98                  4                  4           8            3          15   \n99                  1                  1           2            1           1   \n\n    shopping_carts  purchase_times  favourite_times  \n0                0               1                2  \n1                0               1                2  \n2                0               1                2  \n3                0               1                2  \n4                0               1                2  \n..             ...             ...              ...  \n95               0               2                3  \n96               0               2                3  \n97               0               2                3  \n98               0               2                3  \n99               0               1                0  \n\n[100 rows x 15 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>origin</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>total_logs</th>\n      <th>unique_item_ids_x</th>\n      <th>unique_item_ids_y</th>\n      <th>categories</th>\n      <th>browse_days</th>\n      <th>one_clicks</th>\n      <th>shopping_carts</th>\n      <th>purchase_times</th>\n      <th>favourite_times</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>7</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>24</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>1</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>39</td>\n      <td>20</td>\n      <td>20</td>\n      <td>2</td>\n      <td>9</td>\n      <td>36</td>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>95</th>\n      <td>305280</td>\n      <td>630</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>20</td>\n      <td>4</td>\n      <td>4</td>\n      <td>1</td>\n      <td>3</td>\n      <td>15</td>\n      <td>0</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>96</th>\n      <td>305280</td>\n      <td>630</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>20</td>\n      <td>4</td>\n      <td>4</td>\n      <td>3</td>\n      <td>3</td>\n      <td>15</td>\n      <td>0</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>97</th>\n      <td>305280</td>\n      <td>630</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>20</td>\n      <td>4</td>\n      <td>4</td>\n      <td>8</td>\n      <td>3</td>\n      <td>15</td>\n      <td>0</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>98</th>\n      <td>305280</td>\n      <td>630</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>20</td>\n      <td>4</td>\n      <td>4</td>\n      <td>8</td>\n      <td>3</td>\n      <td>15</td>\n      <td>0</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>99</th>\n      <td>240000</td>\n      <td>2278</td>\n      <td>0.0</td>\n      <td>train</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>100 rows × 15 columns</p>\n</div>"
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data[data['gender'] != 2]\n",
    "data = data[data['age_range'] != -1]\n",
    "data.head(100)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:59:31.022252700Z",
     "start_time": "2024-09-23T13:59:30.433647500Z"
    }
   },
   "id": "d282070810428422",
   "execution_count": 90
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "label\nnan    492617\n0.0    448608\n1.0     41953\nName: count, dtype: int64"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['label'].value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T12:36:22.836798300Z",
     "start_time": "2024-09-23T12:36:22.634666900Z"
    }
   },
   "id": "92134a1df9e4dcf8",
   "execution_count": 67
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "df_train = data[data['origin']=='train'].drop(['origin'],axis=1)\n",
    "df_test = data[data['origin']=='test'].drop(['label','origin'],axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:59:43.604790700Z",
     "start_time": "2024-09-23T13:59:42.741221400Z"
    }
   },
   "id": "4a1f1530bea8234a",
   "execution_count": 91
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 随机采样\n",
    "sample_fraction = 0.2  # 采样比例\n",
    "df_train = df_train.sample(frac=sample_fraction, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T13:59:46.701672600Z",
     "start_time": "2024-09-23T13:59:46.456507600Z"
    }
   },
   "id": "f76c05cbfd4d4472",
   "execution_count": 92
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X = df_train.drop('label',axis=1)\n",
    "y = df_train['label']\n",
    "X_train,X_val,y_train,y_val = train_test_split(X, y, test_size=0.3, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:00:08.732341Z",
     "start_time": "2024-09-23T13:59:48.515458200Z"
    }
   },
   "id": "231aba80b2fab444",
   "execution_count": 93
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import roc_auc_score"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:00:16.507311200Z",
     "start_time": "2024-09-23T14:00:10.694849900Z"
    }
   },
   "id": "a1d1ebf5db12cef1",
   "execution_count": 94
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 在不设置 class_weight 的情况下，随机森林默认给予所有样本相同的权重。如果数据集不平衡，模型可能会偏向于多数类。 设置 class_weight='balance'：这会根据类别频率自动调整权重，使得模型在训练时更加关注少数类。具体来说，权重是 n_samples / (n_classes * np.bincount(y))，其中 n_samples 是样本总数，n_classes 是类别数，np.bincount(y) 是每个类别的样本数。\n",
    "model = RandomForestClassifier(n_estimators=60,max_depth=13, random_state=0,class_weight='balanced')\n",
    "model.fit(X_train,y_train)\n",
    "y_pred=model.predict(X_val)\n",
    "y_proba = model.predict_proba(X_val)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:08:54.238948300Z",
     "start_time": "2024-09-23T14:08:46.440975Z"
    }
   },
   "id": "d678f0a05dc415b3",
   "execution_count": 110
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "验证集auc:  0.6710165275994866\n"
     ]
    }
   ],
   "source": [
    "auc = roc_auc_score(y_val,y_pred)\n",
    "print('验证集auc: ',auc)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:08:54.295986400Z",
     "start_time": "2024-09-23T14:08:54.243952Z"
    }
   },
   "id": "afc91b7279bb790f",
   "execution_count": 111
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集auc:  0.6703616509496915\n"
     ]
    }
   ],
   "source": [
    "y_preds=model.predict(X_train)\n",
    "auc = roc_auc_score(y_train,y_preds)\n",
    "print('训练集auc: ',auc)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:00:32.512243100Z",
     "start_time": "2024-09-23T14:00:31.034266500Z"
    }
   },
   "id": "c0b8e31711f651f5",
   "execution_count": 97
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "user_id: 0.1430848544524181\n",
      "merchant_id: 0.1373150723596904\n",
      "age_range: 0.05656969661468923\n",
      "gender: 0.01914648581530778\n",
      "total_logs: 0.10888340820974367\n",
      "unique_item_ids_x: 0.09617772270246713\n",
      "unique_item_ids_y: 0.10821739951029453\n",
      "categories: 0.06089854061295864\n",
      "browse_days: 0.06235576623677919\n",
      "one_clicks: 0.10205968052978177\n",
      "shopping_carts: 0.008971465557743733\n",
      "purchase_times: 0.0563979928685427\n",
      "favourite_times: 0.039921914529583234\n"
     ]
    }
   ],
   "source": [
    "# 打印特征重要性\n",
    "for name, importance in zip(X_train, model.feature_importances_):\n",
    "    print(f\"{name}: {importance}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:09:02.915287200Z",
     "start_time": "2024-09-23T14:09:02.848245300Z"
    }
   },
   "id": "2413f997a26ec5ba",
   "execution_count": 112
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "['model.kpl']"
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import joblib\n",
    "joblib.dump(model, 'model.kpl')  "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-22T13:53:35.082929900Z",
     "start_time": "2024-09-22T13:53:34.922331500Z"
    }
   },
   "id": "a2c3dec9ff1ea314",
   "execution_count": 77
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "验证集auc:  0.6726182553294471\n"
     ]
    }
   ],
   "source": [
    "from xgboost import XGBClassifier\n",
    "bst = XGBClassifier(n_estimators=100, max_depth=15, learning_rate=1, objective='binary:logistic')\n",
    "# fit model\n",
    "bst.fit(X_train, y_train)\n",
    "y_pred1 =bst.predict(X_val)\n",
    "y_proba1 = bst.predict_proba(X_val)\n",
    "auc1 = roc_auc_score(y_val,y_pred1)\n",
    "print('验证集auc: ',auc1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-23T14:16:24.381670300Z",
     "start_time": "2024-09-23T14:16:19.870538500Z"
    }
   },
   "id": "496c86699834ea9d",
   "execution_count": 120
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
